package cn.ajb.filemanager.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ajb.filemanager.util.Contant;

public class LoginFilter implements Filter {

	public void destroy() {
		// TODO 自动生成的方法存根

	}
	/**
	 * 用户过滤器
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		String uri = request.getRequestURI();
		
		//判断为登录请求
		if(uri.contains("/js")||uri.contains("/css")){
			chain.doFilter(request, response);
		}else{

			if(uri.contains("/login")){
				//登录请求直接放行
				chain.doFilter(request, response);
			}else{
				if(uri.contains("/qr")){
					//是否为是通过QR码扫码进入的 也直接放行
					chain.doFilter(request, response);
				}else{
					//访问的是首页index.jsp是也放行
					if(uri.contains("/index.jsp")){
						chain.doFilter(request, response);
					}else{
						//其他访问需要用户登录
						if(request.getSession().getAttribute("SYS_USER")!=null){
							chain.doFilter(request, response);
						}else{
							//否则转发到登录页面
							//request.setAttribute("error", "您未登录本系统");
							//request.getRequestDispatcher("/index.jsp").forward(request, response);
							response.sendRedirect(request.getContextPath()+"/index.jsp");
						}
					}
				}
			}
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO 自动生成的方法存根

	}

}
